package com.ocl.www.controller;

import com.ocl.www.domain.Article;
import com.ocl.www.domain.User;
import com.ocl.www.service.ArticleService;
import com.ocl.www.service.UserAttentionService;
import com.ocl.www.service.UserService;
import com.ocl.www.utils.StringUntil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by Administrator on 2016/11/3.
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;


    private Logger log = Logger.getLogger(IndexController.class);
    @Autowired
    private UserAttentionService userAttentionService;

    @ApiOperation(value="查找用户的信息", notes="查找用户的粉丝信息，标签信息，文章，get方式")
    @RequestMapping(value = "/getIndexInfo", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,Object> getIndexInfo(){
       Map<String,Object> map = new HashedMap();
        List<User>  userlist = new ArrayList<User>();
        try {
            userlist = userService.selectUsers();
            map.put("userlist",userlist);

            int lc = 0;
            int tc = 0;
            for (User user : userlist){

                //查找用户的粉丝信息
                List<User>  userlist1 = userService.selectFansForUser(user);
                map.put("userlist"+lc,userlist1);

                //查找用户的标签信息
                String usertags = StringUntil.listToString(userAttentionService.queryAttentionOfUser(user.getUserId()));
                map.put("usertags"+lc,usertags);

                //查找用户的文章
                List<Article> articlelist = articleService.selectArticlesOfUser(user);
                map.put("articlelist"+lc,articlelist);

                lc++;
            }
        } catch (Exception e) {

            e.printStackTrace();

        }

        return map;


    }

    @ApiOperation(value="查找用户的信息", notes="查找用户的粉丝信息，标签信息，文章，post方式")
    @RequestMapping(value = "/getIndexInfo2", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,Object> getIndexInfo2(){
        Map<String,Object> map = new HashedMap();
        List<User>  userlist = new ArrayList<User>();
        try {
            userlist = userService.selectUsers();
            map.put("userlist",userlist);

            int lc = 0;
            int tc = 0;
            for (User user : userlist){

                //查找用户的粉丝信息
                List<User>  userlist1 = userService.selectFansForUser(user);
                map.put("userlist"+lc,userlist1);

                //查找用户的标签信息
                String usertags = StringUntil.listToString(userAttentionService.queryAttentionOfUser(user.getUserId()));
                map.put("usertags"+lc,usertags);

                //查找用户的文章
                List<Article> articlelist = articleService.selectArticlesOfUser(user);
                map.put("articlelist"+lc,articlelist);

                lc++;
            }
        } catch (Exception e) {

            e.printStackTrace();

        }

        return map;


    }

    /***
     * description : 判断一个用户请求是否已经登录
     * @return
     */

    @ApiOperation(value="判断一个用户请求是否已经登录", notes="判断一个用户请求是否已经登录")
    @RequestMapping(value = "/isLogin",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,Object> isLogin(){

        Map<String,Object> map = new HashMap<String,Object>();

        try{
            log.info(SecurityUtils.getSubject().getPrincipals().asList());

            Set<?> s = SecurityUtils.getSubject().getPrincipals().asSet();
            Object[] obj = new Object[2];
            int obji = 0;
            Iterator<?> it = s.iterator();
            while(it.hasNext()){
                obj[obji] = it.next();
                log.info(obj.getClass());
                log.info(obj.toString());
                obji++;
            }
            Map<String,String> map1 = (Map<String, String>) obj[1];
//           log.info(map.get("realName"));
//			log.info(map.get("nickName"));
//			log.info(map.get("sex"));
//			log.info(map.get("mobile"));
//			log.info(map.get("userId"));
//			log.info(map.get("email"));
//          log.info(map.get("account"));
            User user = new User();
            user.setUserId(Integer.valueOf(map1.get("userId")));
//            try {
//                user.setNickName(new String(map1.get("nickName").getBytes(),"UTF-8"));
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//            user.setSex(map1.get("sex"));
//            user.setEmail( map1.get("email"));
//            user.setUserName( map1.get("account"));
//            user.setMobile(map1.get("mobile"));

            User user1 = new User();
            try {
                user1 = userService.findUserInfo(user);
            } catch (Exception e) {
                e.printStackTrace();
            }


            map.put("info","s");
            map.put("userinfo",user1);
        }catch (NullPointerException e){
            map.put("info","e");
            map.put("errorinfo",e.getMessage());

        }
        return  map;

    }


    /**
     *
     * logout
     */
    @ApiOperation(value="用户登出", notes="用户登出")
    @RequestMapping(value = "/userlogout", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,Object> userlogout(){
        Map<String,Object> map = new HashMap<String,Object>();
        SecurityUtils.getSubject().logout();
        map.put("info","0");
        map.put("tip","logout succ");
        return map;
    }


}
